package com.example.shaomengjie.smvp.presenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.example.shaomengjie.smvp.bean.GuideBean;
import com.example.shaomengjie.smvp.framework.presenter.ActivityPresenter;
import com.example.shaomengjie.smvp.framework.view.AppDelegate;
import com.example.shaomengjie.smvp.utils.Constant;
import com.example.shaomengjie.smvp.view.SplashADDelegate;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shaomengjie on 2017/6/23.
 */

public class SplashADActivity extends ActivityPresenter<SplashADDelegate> {
    private List<GuideBean> data;
    @Override
    protected Class<SplashADDelegate> getDelegateClass() {
        return SplashADDelegate.class;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        data = (List<GuideBean>) getIntent().getExtras().get(Constant.AD);
        if (data == null || data.size()==0){return;}
        Log.e(TAG,data.size()+"");
        viewDelegate.loadADImageView(data);
        Observable.interval(2, TimeUnit.SECONDS)
                .take(data.size())
                .map(new Function<Long, Integer>() {
                    @Override
                    public Integer apply(@NonNull Long aLong) throws Exception {
                        return Integer.parseInt(String.valueOf(aLong));
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(@NonNull Integer integer) throws Exception {
                        Log.e(TAG,integer+"");
                        if (integer == data.size()-1){
                            startActivity(new Intent(SplashADActivity.this,MainActivity.class));
                        }else {
                            viewDelegate.showImageView(integer);
                        }
                    }
                });
    }
}
